package gui.ulohy;

import gui.hodnoceni.HodnoceniPanel;
import gui.obrazky.ObrazekPanel;
import gui.obrazky.SmajlikLabel;
import gui.skolnikwin.SkolnikWinManager;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import utils.Nastroje;

import model.NastaveniUlohy;
import model.UlohyEnum;
import model.Zadani;

/**
 * Reprezentuje ulohu s ceskym jazykem.
 * @author schovjan
 * @date Jan 9, 2012
 */
public class CeskyJazyk extends JDialog {

    private static final long serialVersionUID = 1L;
    private SkolnikWinManager winManager = null;
    private ObrazekPanel obrazek = null;
    private SmajlikLabel smajlik = null;
    private NastaveniUlohy nastaveni = null;
    private JPanel pOvladaniHodnoceni = null;
    private JPanel pOvladani = null;
    private Dimension velikost = new Dimension(800, 600);
    private JButton btnKonec = new JButton("Ukončit český jazyk");
    private JLabel lText = new JLabel("zde je zadani"); // label pro text zadani
    private List<String> chyby;
    private List<Zadani> priklady;
    private int index = 0;
    private Zadani slovo;
//    private int pocetSpravne = 0;	//pocet spravnych odpovedi
    private int pocetSpatne = 0;	//pocet spatnych odpovedi


    /**
     * Konstruktor
     * @param nastaveni
     * @throws IOException
     */
    public CeskyJazyk(SkolnikWinManager winManager, NastaveniUlohy nastaveni, List<Zadani> priklady) throws Exception {
	super();
	this.nastaveni = nastaveni;
	this.winManager = winManager;
	this.priklady = priklady;
	Collections.shuffle(priklady);
	createGUI();
	initZadani();
	chyby = new ArrayList<String>();
    }


    /**
     * Vytvoreni GUI
     * @throws IOException
     */
    private void createGUI() throws Exception {
	nastavPanelOvladani();
	lText.setMinimumSize(new Dimension(this.velikost.width - 40, 80));
	lText.setVerticalAlignment(SwingConstants.CENTER);
	lText.setHorizontalAlignment(SwingConstants.CENTER);
	lText.setOpaque(true);
	lText.setBackground(Color.WHITE);
	lText.setBorder(BorderFactory.createLineBorder(Color.BLACK));
	lText.setFont(Nastroje.fVelky);
	obrazek = new ObrazekPanel(Nastroje.getNahodnyObrazek(), nastaveni.getPocetDiluObrazku());
	// konec cestiny
	btnKonec.setFont(Nastroje.fStredni);
	btnKonec.setBackground(Nastroje.cCervena);
	btnKonec.setMinimumSize(new Dimension(90, 30));
	btnKonec.addActionListener(new AbstractAction() {

	    private static final long serialVersionUID = 1L;


	    @Override
	    public void actionPerformed(ActionEvent e) {
		setVisible(false);
	    }
	});
	
	smajlik = new SmajlikLabel();
	pOvladaniHodnoceni = new JPanel();
	pOvladaniHodnoceni.setLayout(new BoxLayout(pOvladaniHodnoceni, BoxLayout.Y_AXIS));
	pOvladaniHodnoceni.setBackground(Nastroje.cZluta);
	pOvladaniHodnoceni.add(pOvladani);
	pOvladaniHodnoceni.add(smajlik);
	
	
	GroupLayout layout = new GroupLayout(this.getContentPane());
	layout.setAutoCreateGaps(true);
	layout.setAutoCreateContainerGaps(true);
	this.setLayout(layout);
	layout.setHorizontalGroup(layout.createSequentialGroup()
		.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
			.addComponent(lText)
			.addGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
					.addComponent(obrazek))
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
					.addComponent(pOvladaniHodnoceni)
					.addComponent(btnKonec, GroupLayout.Alignment.CENTER)))));
	layout.setVerticalGroup(layout.createSequentialGroup()
		.addComponent(lText)
		.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
			.addComponent(obrazek)
			.addGroup(layout.createSequentialGroup()
				.addComponent(pOvladaniHodnoceni)
				.addComponent(btnKonec))));
	this.setSize(velikost);
	this.getContentPane().setBackground(Nastroje.cZluta);
	this.setTitle("Procvičujeme si... ČESKÝ JAZYK");
	this.setLocationRelativeTo(null);
	this.repaint();
	this.setModal(true);
    }

    
    /**
     * Nastavi panel s ovladacimi prvky 
     * pro odpovedi
     * @return
     */
    private void nastavPanelOvladani() {
	int y = 0;
	List<UlohyEnum> ulohy = nastaveni.getZadani();
	pOvladani = new JPanel(new GridBagLayout());
	pOvladani.setBackground(Nastroje.cZluta);
	GridBagConstraints c = new GridBagConstraints();
	c.insets = new Insets(5,5,5,5);
	c.fill = GridBagConstraints.BOTH;
	
	if (ulohy.contains(UlohyEnum.VS_B)
		|| ulohy.contains(UlohyEnum.VS_L)
		|| ulohy.contains(UlohyEnum.VS_M)
		|| ulohy.contains(UlohyEnum.VS_P)
		|| ulohy.contains(UlohyEnum.VS_S)
		|| ulohy.contains(UlohyEnum.VS_V)
		|| ulohy.contains(UlohyEnum.VS_Z)) {
	    c.gridx = 0;
	    c.gridy = y;
	    pOvladani.add(getTlacitko("i"), c);
	    c.gridx = 1;
	    pOvladani.add(getTlacitko("y"), c);
	    y++;
	    c.gridx = 0;
	    c.gridy = y;
	    pOvladani.add(getTlacitko("í"), c);
	    c.gridx = 1;
	    pOvladani.add(getTlacitko("ý"), c);
	    y++;
	}
	if (ulohy.contains(UlohyEnum.uU)) {
	    c.gridx = 0;
	    c.gridy = y;
	    pOvladani.add(getTlacitko("u"), c);
	    c.gridx = 1;
	    pOvladani.add(getTlacitko("ů"), c);
	    c.gridx = 2;
	    pOvladani.add(getTlacitko("ú"), c);
	    y++;
	}
    }

    
    /**
     * Vraci inicializovane tlacitko
     * @param popisek
     * @return
     */
    private JButton getTlacitko(final String popisek) {
	JButton btn = new JButton();
	btn.setPreferredSize(new Dimension(80,50));
	btn.setFont(Nastroje.fVelky);
	btn.setAction(new AbstractAction(popisek) {
	    
	    private static final long serialVersionUID = 1L;

	    @Override
	    public void actionPerformed(ActionEvent arg0) {
		odpovedet(popisek);
	    }
	});
	return btn;
    }
    
    
    /**
     * Odpovi 
     */
    private void odpovedet(String odpoved) {
	boolean spravne = slovo.getOriginal().equals(slovo.getUpravene().replace("_", odpoved)); 
	if (spravne) {
	    smajlik.usmat();
	    obrazek.removeCernePole();
//	    pocetSpravne++;
	} else {
	    smajlik.smutnit();
	    chyby.add(slovo.getUpravene().replace("_", odpoved));
	    if (nastaveni.isSkryvat()) {
		obrazek.addCernePole();
	    }
	    pocetSpatne++;
	}
	if (obrazek.getPocetDiluZbyvajicich() == 0) {
	    // byl odkryt cely obrazek
	    pOvladaniHodnoceni.removeAll();
	    pOvladaniHodnoceni.add(new HodnoceniPanel(nastaveni.getPocetDiluObrazku(), pocetSpatne, chyby));
	} else {
	    // zbyvaji cerna pole zakryvajici obrazek
	    if (spravne) {
		initZadani();
	    }
	}
    }
    

    /**
     * Vrati velikost panelu
     * @return
     */
    public Dimension getVelikostPanelu() {
	return velikost;
    }


    /**
     * Nastavi novy retezec
     */
    private void initZadani() {
	slovo = priklady.get(index++ % priklady.size());
	lText.setText(slovo.getUpravene());
    }
}
